package com.gzdh.device.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gzdh.device.system.dto.UserAuthInfoDTO;
import com.gzdh.device.system.entity.SysPermission;
import com.gzdh.device.system.entity.SysUser;
import com.gzdh.device.system.mapper.SysUserMapper;
import com.gzdh.device.system.model.MenuTreeModel;
import com.gzdh.device.system.service.SysPermissionService;
import com.gzdh.device.system.service.SysUserService;
import com.gzdh.device.system.util.FindsMenuChildrenUtil;
import com.gzdh.device.system.vo.AccountAllVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {


    @Autowired
    SysPermissionService sysPermissionService;

    @Autowired
    SysUserMapper sysUserMapper;


    @Override
    public UserAuthInfoDTO loadUserByUsername(String userName) throws UsernameNotFoundException {
        UserAuthInfoDTO userAuthInfoDTO = new UserAuthInfoDTO();
        SysUser sysUser = this.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUserName, userName));
        if (sysUser == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        BeanUtil.copyProperties(sysUser, userAuthInfoDTO);
        if (ObjectUtils.equals(sysUser.getUserType(), 0)) {
            List<SysPermission> sysPermissionList = sysPermissionService.list();
            userAuthInfoDTO.setPermissions(sysPermissionList);
            if (CollectionUtil.isNotEmpty(sysPermissionList)) {
                Set<String> s = sysPermissionList.stream().filter(sysPermission -> (sysPermission.getType() == 2 || sysPermission.getType() == 1))
                        .map(SysPermission::getPermission).collect(Collectors.toSet());
                s.remove(null);
                s.remove("");
                userAuthInfoDTO.setPerms(s);
            }
        } else {
            System.out.println("普通用户");
        }
        return userAuthInfoDTO;
    }

    @Override
    public IPage<AccountAllVo> getAccountAllByPage(int pageNo, int pageSize) {
        IPage<SysUser> sysUserIPage = new Page<>();
        sysUserIPage.setCurrent(pageNo);
        sysUserIPage.setSize(pageSize);
        return sysUserMapper.getSysUserListByPage(sysUserIPage);
    }

    @Override
    public List<MenuTreeModel> getUserMenu(String userId) {
        SysUser sysUser = this.getById(userId);
        List<SysPermission> sysPermissionList = sysUserMapper.getSysUserRoleByRoleId(sysUser.getRoleId(), "0,1");
        List<MenuTreeModel> listResult = FindsMenuChildrenUtil.wrapTreeDataToTreeList(sysPermissionList);
        return listResult;
    }


    @Override
    public List<String> getPermCode(String userId) {
        SysUser sysUser = this.getById(userId);
        List<String> stringList = new ArrayList<>();
        List<SysPermission> sysPermissionList = sysUserMapper.getSysUserRoleByRoleId(sysUser.getRoleId(), "0,1,2");
        for (SysPermission goo : sysPermissionList) {
            if (StringUtils.isEmpty(goo.getPermission())) {
                continue;
            }
            stringList.add(goo.getPermission());
        }
        if (stringList.isEmpty()) {
            stringList.add("test");
        }
        return stringList;
    }
}
